package com.hoveagle.kafka.consumer;

import com.hoveagle.kafka.exceptions.ReadFileException;
import com.hoveagle.kafka.utils.FileUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * define kafka configuration
 * @author hoveagle
 */
public enum KafkaConfig {
    CONFIG1("topic1", "kafkaConfig1.properties"),CONFIG2("topic2,topic3", "kafkaConfig2.properties");

    private String[] topics;
    private String kafkaConfigFile;
    private KafkaConfig(String topics, String configFile){
        this.topics = topics.split(",");
        this.kafkaConfigFile = configFile;
    }

    public String[] getTopics(){
        return topics;
    }

    public Properties getKafkaConfig() throws ReadFileException {
        FileInputStream fileInputStream = null;
        try {
            Properties properties = new Properties();
            String realPath = String.format("%s%s%s%s%s", System.getProperty("user.dir"), File.separator,"conf", File.separator,this.kafkaConfigFile);
            fileInputStream = new FileInputStream(realPath);
            properties.load(fileInputStream);
            return  properties;
        } catch (IOException e) {
            throw new ReadFileException(e.getMessage(),e.getCause());
        } finally {
            FileUtil.close(fileInputStream);
        }

    }
}
